<?php
session_start();
if(isset($_SESSION['lang']))
    require_once "./language_files/signUpServer_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/signUpServer_en.php";
require_once "statics.php";
require_once "recaptchalib.php";
if(isset($_GET['unconfirmed_mail']))
{
    if(preg_match("/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/",$_GET['unconfirmed_mail']) == 0)
    {
        echo $lang['error_1'] . __LINE__;
        exit();
    }

    $rsEmail=rsSelect("email","member",array("email=",$_GET['unconfirmed_mail']));
    if(is_string($rsEmail))
    {
        echo $rsEmail . $lang['error_1'] . __LINE__;
        exit();
    }
    if(mysql_num_rows($rsEmail) == 0)
        echo "1";
    else
        echo "0";
}

if(isset($_POST['first']))
{
    $rsEmail=rsSelect("email,activated","member",array("email=",$_POST['email']));
    if(is_string($rsEmail))
    {
        echo $rsEmail . $lang['error_1'] . __LINE__;
        exit();
    }
    if(mysql_num_rows($rsEmail) > 0)
    {
        $row=mysql_fetch_assoc($rsEmail);
        if($row['activated'] == 0)
        {
            echo $lang['alert_1'] . __LINE__;
            exit();
        }
    }
    if($_POST['password'] != $_POST['confirm_password'] || strlen($_POST['password']) < 8)
    {
        echo $lang['error_1'] . __LINE__;
        exit();
    }
    $private_key="6LdSNtMSAAAAAIgqZUd2lVemKMhnsYx18cnkWiSq";
    $verify=recaptcha_check_answer($private_key,$_SERVER['REMOTE_ADDR'],$_POST['recaptcha_challenge_field'],$_POST['recaptcha_response_field']);
    if(!$verify->is_valid)
    {
        echo $lang['alert_2'] . __LINE__;
        exit();
    }
    $password=sHashNewPassword($_POST['email'],$_POST['password']);
    $iRet=iInsert("member",array("first_name"=>$_POST['first'],"last_name"=>$_POST['last'],"email"=>$_POST['email'],"password"=>$password));
    if(is_string($iRet))
    {
        echo $iRet . "ERROR CODE: 0" . __LINE__;
        exit();
    }
	$salt=hash("sha256",uniqid(mt_rand(),true).strtolower($_POST['email']));
	$sHashed=$salt.$_POST['email'];
	for($i=0;$i<5000;$i++)
		$sHashed=hash("sha256",$sHashed);
	$sHashed=$salt.$sHashed;
	$to=$_POST['email'];
    $subject=$lang['subject'];
    $message=$lang['msg'] . " http://arolis.com/confo/index.php?user=" . $sHashed . "&new=TRUE";
    $from=$lang['from'];
    $bRet=mail($to,$subject,$message,$from) or die("MAIL SERVER ERROR CODE 0" . __LINE__);
    if(!$bRet)
    {
        echo "MAIL SERVER ERROR CODE 0" . __LINE__;
        exit();
    }
    echo "1";
}
?>
